home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files for Lab 5 / 3N+1 Sequence next >
Text File  |  1994-04-16  |  2KB  |  55 lines

  1. ; This file contains an assembly language program that
  2. ; computes the "3N+1" sequence starting from any integer
  3. ; N.  This sequence is generated by applying the rule:
  4. ;        IF N is even 
  5. ;             THEN replace N with N divided by 2
  6. ;             ELSE replace N with 3 times N plus 1
  7. ; over and over until N becomes equal to 1.  There are
  8. ; some possible problems:  N might become too big to be
  9. ; represented by a 16-bit number.  Or, it might be that
  10. ; N never becomes 1, so that the sequence never ends.
  11. ; These problems are simply ignored in this version of
  12. ; the program.
  13.  
  14. @PC 100 ; This tells the assemble: Put the number 100 into
  15.         ; the PC register when this program is loaded.
  16.         ; (This is a special assembler command, and PC is the
  17.         ; only register that can be used in this way.)
  18.  
  19. @100  ; This tells the assembler: Put the next instruction
  20.       ; in location 100.  (Usually, it starts at location 0.)
  21.  
  22. NextN : LOD N      ; Get the number N.
  23.         SUB-C 1    ; If it is 1, we are done.
  24.         JMZ Done
  25.         LOD N      ; Otherwise, test if N is odd or even.
  26.         SHR        ; (If it is even, the last bit is zero; if it
  27.         JMF Odd    ; is odd, that bit is one.  The shift-right
  28.                    ; operation drops this bit into the flag
  29.                    ; register, where it can be tested.  At the
  30.                    ; same time, the value remaining in the AC
  31.                    ; is N/2.)
  32.  
  33. Even:   STO N      ; Number in the AC is already equal to N / 2.
  34.         JMP NextN  ; Replace N with this number and jump back to
  35.                    ; the start of the loop.
  36.  
  37. Odd:    LOD N      ; Compute 3 times N plus 1, replace N with
  38.         ADD N      ; the result and return to start.
  39.         ADD N
  40.         ADD-C 1
  41.         STO N
  42.         JMP NextN
  43.  
  44. Done:   HLT
  45.  
  46. N:      7         ; The memory location named "N" is loaded
  47.                   ; with the number "7" rather than with an
  48.                   ; assembly language instruction.  The initial
  49.                   ; value of N when the program is run is the
  50.                   ; starting point for the "3N+1" sequence.
  51.                   ; Put a different value into N before
  52.                   ; loading the program to start with a different
  53.                   ; value.  (In this program, without any
  54.                   ; modifications, N is 115.)
  55.